import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

public class DocumentSampling {
	
	public List<String> fetchDoc(String t, String url){
		 List<String> s=new ArrayList<String>();
		 return s;
		
	}
	public void storeDoc(String URL){
		
	}
	public void getDoc(){
		
	}
	public static void buildTextDataBase(HashMap<String, List<String>> queryUrlMap, Set<String> querySet){
		
		Set<String> URLSet= new TreeSet<String>();
		
		for(String s:querySet){
			if(queryUrlMap.containsKey(s)){
			URLSet.addAll(queryUrlMap.get(s));
			}
		}
		int count=0;
		System.out.println(URLSet.size());
		
		for(String url:URLSet){
			
			System.out.println(url);
			
			String s=HTMLReader.getHTMLText(url);
			PrintWriter sumOut;
			count++;
			try {
				sumOut = new PrintWriter(
						new BufferedWriter(new FileWriter("test/"+count+".txt")));
				sumOut.println(s);
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("text output error");
			}
			
		}
		
	}
	private static String formatFilename(String filename)
	{
		filename = filename.replace('\\', '/').replaceAll("https?://", "").replaceAll("/$", "");
		try {
			filename = URLEncoder.encode(filename, "UTF-8").replaceAll("%2F", "/");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return filename;
	}

	public static HashMap<Category, Set<String>> sample(Hierarchy H, List<Category> Lst ,String url){
		
		Queue<Category> Q=new LinkedList<Category>();
		
		//category
		
		HashMap<Category, Set<String>> queryMap=new HashMap <Category, Set<String>>();
		List<Category> C=new ArrayList<Category> ();
		/*some doubts related to map contains*/
		
		for(Category TT:Lst){
			
			C.add(H.catMap.get(TT.name));
			
		}
	
		
		for(Category D: C){
			Q.add(D);
		}
		
		while(!Q.isEmpty()){
			
			Category T=Q.poll();
			
		    //add parent to Queue
			
			if(T.parent!=null && !Q.contains(T.parent)){
				Q.add(T.parent);
			}
			
			/*add query to itself*/
			
			if(!T.children.isEmpty()){
				
				
					for(Category P:T.children){
						Set<String> TS=new TreeSet<String>();
						if(queryMap.containsKey(T)){
							TS=queryMap.get(T);
							TS.addAll(P.probeQueries);
							queryMap.put(T, TS);
						}else{
				    	  TS.addAll(P.probeQueries);
				    	  queryMap.put(T, TS);
						}
					}
				
				
			}
			
			/*contribute to its parent*/
			if(T.parent!=null){
				
				if(queryMap.containsKey(T.parent)){
					
					Set<String> TS;
					TS=queryMap.get(T.parent);
					TS.addAll(queryMap.get(T));
					queryMap.put(T.parent, TS);
				}
				else{
					
					queryMap.put(T.parent, queryMap.get(T));
				}
				
			}
			
		/*test*/
			if(queryMap.containsKey(T)){
				System.out.println("++++++"+T.name+"++++++++++++");
				for (String s:queryMap.get(T)){
					
					System.out.println(s);
				}
			}
		}
		return queryMap;	
	}
	
	
	
}
